/*
 * Copyright (c) 2016  athou（cai353974361@163.com）.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.repo.logger;


import androidx.annotation.NonNull;

import timber.log.Timber;

/**
 * Log统一管理类
 *
 * @author athou
 */
@Deprecated
public class LogUtil {
    private static LogPrinter printer = new LogPrinter();

    private LogUtil() {
    }

    public static class LogConfig extends Timber.DebugTree {
        @Override
        protected void log(int priority, String tag, @NonNull String message, Throwable t) {
            super.log(priority, tag, message, t);
        }

        @Override
        protected boolean isLoggable(@NonNull String tag, int priority) {
            return super.isLoggable(tag, priority);
        }

        @Override
        protected String formatMessage(@NonNull String message, @NonNull Object[] args) {
            return super.formatMessage(message, args);
        }
    }

    public static class LogPrinter {
        public void i(String msg, Object... args) {
            Timber.i(msg, args);
        }

        public void i(Throwable e, String msg, Object... args) {
            Timber.i(e, msg, args);
        }

        public void i(Throwable e) {
            Timber.i(e);
        }

        public void d(String msg, Object... args) {
            Timber.d(msg, args);
        }

        public void d(Throwable e, String msg, Object... args) {
            Timber.d(e, msg, args);
        }

        public void d(Throwable e) {
            Timber.d(e);
        }

        public void v(String msg, Object... args) {
            Timber.v(msg, args);
        }

        public void v(Throwable e, String msg, Object... args) {
            Timber.v(e, msg, args);
        }

        public void v(Throwable e) {
            Timber.v(e);
        }

        public void w(String msg, Object... args) {
            Timber.w(msg);
        }

        public void w(Throwable e, String msg, Object... args) {
            Timber.w(e, msg, args);
        }

        public void w(Throwable e) {
            Timber.w(e);
        }

        public void e(String msg, Object... args) {
            Timber.e(msg, args);
        }

        public void e(Throwable e, String msg, Object... args) {
            Timber.e(e, msg, args);
        }

        public void e(Throwable e) {
            Timber.e(e);
        }

        public void wtf(String msg, Object... args) {
            Timber.wtf(msg, args);
        }

        public void wtf(Throwable e, String msg, Object... args) {
            Timber.wtf(e, msg, args);
        }

        public void wtf(Throwable e) {
            Timber.wtf(e);
        }

        public void log(int priority, @NonNull String message, Object... args) {
            Timber.log(priority, message, args);
        }

        public void log(int priority, Throwable e, @NonNull String message, Object... args) {
            Timber.log(priority, e, message, args);
        }

        public void log(int priority, Throwable e) {
            Timber.log(priority, e);
        }
    }

    public static void init(@NonNull LogConfig config) {
        Timber.plant(config);
    }

    public static LogPrinter tag(String tag) {
        Timber.tag(tag);
        return printer;
    }

    public static void i(String msg, Object... args) {
        tag(tag()).i(msg, args);
    }

    public static void i(Throwable e, String msg, Object... args) {
        tag(tag()).i(e, msg, args);
    }

    public static void i(Throwable e) {
        tag(tag()).i(e);
    }

    public static void d(String msg, Object... args) {
        tag(tag()).d(msg, args);
    }

    public static void d(Throwable e, String msg, Object... args) {
        tag(tag()).d(e, msg, args);
    }

    public static void d(Throwable e) {
        tag(tag()).d(e);
    }

    public static void v(String msg, Object... args) {
        tag(tag()).v(msg, args);
    }

    public static void v(Throwable e, String msg, Object... args) {
        tag(tag()).v(e, msg, args);
    }

    public static void v(Throwable e) {
        tag(tag()).v(e);
    }

    public static void w(String msg, Object... args) {
        tag(tag()).w(msg);
    }

    public static void w(Throwable e, String msg, Object... args) {
        tag(tag()).w(e, msg, args);
    }

    public static void w(Throwable e) {
        tag(tag()).w(e);
    }

    public static void e(String msg, Object... args) {
        tag(tag()).e(msg, msg);
    }

    public static void e(Throwable e, String msg, Object... args) {
        tag(tag()).e(e, msg, args);
    }

    public static void e(Throwable e) {
        tag(tag()).e(e);
    }

    public static void wtf(String msg, Object... args) {
        tag(tag()).wtf(msg, args);
    }

    public static void wtf(Throwable e, String msg, Object... args) {
        tag(tag()).wtf(e, msg, args);
    }

    public static void wtf(Throwable e) {
        tag(tag()).wtf(e);
    }

    public static void log(int priority, @NonNull String message, Object... args) {
        tag(tag()).log(priority, message, args);
    }

    public static void log(int priority, Throwable e, @NonNull String message, Object... args) {
        tag(tag()).log(priority, e, message, args);
    }

    public static void log(int priority, Throwable e) {
        tag(tag()).log(priority, e);
    }

    private static String tag() {
        StackTraceElement stackTraceElement = new Exception().getStackTrace()[2];
        String str = stackTraceElement.getClassName();
        return "[" + str.substring(1 + str.lastIndexOf(".")) + " " + stackTraceElement.getMethodName()
                + ":" + stackTraceElement.getLineNumber() + "]";
    }

    private static String checkNullLog(String logMsg) {  //对log进行判空  防止NP
        return logMsg == null ? "" : logMsg;
    }
}
